<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/12
 * Time: 16:10
 */
namespace App\Models;

Class Gift_recive extends BaseModel
{

    protected $table = 'gift_receive';
    public $timestamps = false;


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    /**
     * 得到免费领取礼物数据
     */
    public function getGiftRecivedata($uid){
         $where['uid'] = $uid;
         $where['date'] = date("y-m-d",time());
         $data = $this->getOne($where);
         if (empty($data)) {
              // 读取后台配置
              $config_gift = new Config_gift();
              $config_giftdata = $config_gift->getOne([]);
              $inserdata['gift_1_waittime'] = $config_giftdata['gift_1_waittime'];
              $inserdata['gift_2_waittime'] = $config_giftdata['gift_2_waittime'];
              $inserdata['gift_3_waittime'] = $config_giftdata['gift_3_waittime'];
              $inserdata['gift_4_waittime'] = $config_giftdata['gift_4_waittime'];
              $inserdata['uid'] = $uid;
              $inserdata['date'] =  $where['date'];
              $inserdata['waittime'] = 0;
              $res = $this->add($inserdata);
              if ($res) {
                  $inserdata['giftnow'] = 1;
                  $inserdata['gift_waitttime'] = $inserdata['gift_1_waittime'];
                  return $inserdata;
              }
         }
         if ($data['gift_1']==0) {
             $data['giftnow'] = 1;
         }else if ($data['gift_2']==0) {
             $data['giftnow'] = 2;
         }else if ($data['gift_3']==0) {
             $data['giftnow'] = 4;
         }else if  ($data['gift_4']==0) {
             $data['giftnow'] = 5;
         }else if  ($data['gift_5']==0) {
             $data['giftnow'] = 6;
         }
         $data['gift_waitttime'] = $data['gift_'.$data['giftnow'].'_waittime'];
         $remaintime = $data['gift_waitttime']-$data['waittime'];
         $data['remaintime'] = $remaintime;
         if ($remaintime == 0) {
              $data['gift_'.$data['giftnow']] = 1;
         }
         return $data;
    }


    public function updateTime(){
           $where['date'] = date("Y-m-d",time());
           $where['uid']  = session('loginId');
           $receiveinfo = $this->getOne($where);
           if ($receiveinfo['gift_1']==0) {
                $data['giftnow'] = 1;
           }else if ($receiveinfo['gift_2']==0) {
                $data['giftnow'] = 2;
           }else if ($receiveinfo['gift_3']==0) {
                $data['giftnow'] = 4;
           }else if  ($receiveinfo['gift_4']==0) {
                $data['giftnow'] = 5;
           }else if  ($receiveinfo['gift_5']==0) {
                $data['giftnow'] = 6;
           }
           $forwaittime = $receiveinfo['gift_'.$data['giftnow']."_waittime"];
           if ($receiveinfo['waittime'] == $forwaittime){
                   return ['code'=>201,'msg'=>'计时已到','data'=>$data['giftnow']];
           }
           $up['waittime'] = $receiveinfo['waittime']+1;
           $this->up($where,$up);
           return ['code'=>200,'msg'=>'计时加一'];
    }

    /**
     * 领取在线礼物
     */
    public function receivegift(){
        $where['date'] = date("Y-m-d",time());
        $where['uid']  = session('loginId');
        $receiveinfo = $this->getOne($where);
        if ($receiveinfo['gift_1']==0) {
            $data['giftnow'] = 1;
        }else if ($receiveinfo['gift_2']==0) {
            $data['giftnow'] = 2;
        }else if ($receiveinfo['gift_3']==0) {
            $data['giftnow'] = 4;
        }else if  ($receiveinfo['gift_4']==0) {
            $data['giftnow'] = 5;
        }else if  ($receiveinfo['gift_5']==0) {
            $data['giftnow'] = 6;
        }
        $forwaittime = $receiveinfo['gift_'.$data['giftnow']."_waittime"];
        if ($receiveinfo['waittime'] < $forwaittime){
            return ['code'=>201,'msg'=>'计时未到'];
        }
        $prize_arr = array(
            '0' => array('id'=>1,'prize'=>'获得积分奖励','v'=>1000),
            '1' => array('id'=>2,'prize'=>'获得经验奖励','v'=>600),
            '2' =>array('id'=>3,'prize'=>'获得玫瑰','v'=>40),
            '3' =>array('id'=>4,'prize'=>'获得M币','v'=>10),
        );
        $arr = [];
        foreach ($prize_arr as $key => $val) {
            $arr[$val['id']] = $val['v'];
        }
        // 计算下次礼物领取时间
        $next = $data['giftnow']+1;
        $data['w_time'] = $receiveinfo['gift_'.$next.'_waittime'];
        $rid = get_rand($arr);
        $prizeid = $prize_arr[$rid-1]['id'];
        $member = new Member();
        $memberinfo = $member->getOne(['id'=>session('loginId')]);
        if ($prizeid==1) {
             $score = rand(1,100);
             $memberupdata['score'] = $memberinfo['score']+$score;
        }
        if ($prizeid==2) {
            $exp = rand(1,100);
            $memberupdata['experience'] = $memberinfo['experience']+$exp;
        }
//        if ($prizeid==3) {
//            $num = rand(1,100);
//            $memberupdata['score'] = $score;
//        }
        if ($prizeid==4) {
            $m = rand(1,50);
            $memberupdata['m'] =$memberinfo['m']+$m;
        }
        $res = $member->up(['id'=>session('loginId')],$memberupdata);
        if ($res){
             $receiveinfoup["gift_".$data['giftnow']] = 1;
             $receiveinfoup['waittime'] = 0;
             $this->up($where,$receiveinfoup);
             return ['code'=>200,'msg'=>'领取成功','data'=>$data];
        }
        return ['code'=>400,'msg'=>'领取失败'];
    }
}